import './public_path';
import { createApp } from 'vue';
import Cookies from 'js-cookie';
import ElementPlus from 'element-plus';
// import locale from 'element-plus/lib/locale/lang/zh-cn'; // 中文语言
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
import '@/assets/styles/index.scss'; // global css
import App from './App';
import store from './store';
import router from './router';
import directive from './directive'; // directive
// 注册指令
import plugins from './plugins'; // plugins
import { download } from '@/utils/request';
import VXETable from 'vxe-table';
import 'vxe-table/lib/style.css';

import usePermissionStore from '@/store/modules/permission';
// import VueAMap, {initAMapApiLoader} from '@vuemap/vue-amap';
// import '@vuemap/vue-amap/dist/style.css'

// svg图标
import SvgIcon from '@/components/SvgIcon';
import elementIcons from '@/components/SvgIcon/svgicon';
import './assets/icons'; // icon
import './permission'; // permission control
import { useDict } from '@/utils/dict';
import {
  addDateRange,
  handleTree,
  parseTime,
  resetForm,
  selectDictLabel,
  selectDictLabels,
} from '@/utils/ruoyi';

// 分页组件
import Pagination from '@/components/Pagination';
// 自定义表格工具组件
import RightToolbar from '@/components/RightToolbar';
// 文件上传组件
import FileUpload from '@/components/FileUpload';
// 图片上传组件
import ImageUpload from '@/components/ImageUpload';
// 图片预览组件
import ImagePreview from '@/components/ImagePreview';
// 自定义树选择组件
import TreeSelect from '@/components/TreeSelect';
// 字典标签组件
import DictTag from '@/components/DictTag';

// 全局方法挂载
let instance = null;
//  initAMapApiLoader({
//   key: 'a0a7eb7a2b8907f80122bee3aaebc8fc',
//
//   // securityJsCode: 'ba47176c2791f15abeff7ef9ee9c5f76', // 新版key需要配合安全密钥使用
// })

function render(props = {}) {
  const { container, subsysList, state } = props
  instance = createApp(App);

  console.log(instance, '<<<===instance实例对象-----------vue实例对象');
  instance.config.globalProperties.useDict = useDict;
  instance.config.globalProperties.download = download;
  instance.config.globalProperties.parseTime = parseTime;
  instance.config.globalProperties.resetForm = resetForm;
  instance.config.globalProperties.handleTree = handleTree;
  instance.config.globalProperties.addDateRange = addDateRange;
  instance.config.globalProperties.selectDictLabel = selectDictLabel;
  instance.config.globalProperties.selectDictLabels = selectDictLabels;

  // 全局组件挂载
  instance.component('DictTag', DictTag);
  instance.component('Pagination', Pagination);
  instance.component('TreeSelect', TreeSelect);
  instance.component('FileUpload', FileUpload);
  instance.component('ImageUpload', ImageUpload);
  instance.component('ImagePreview', ImagePreview);
  instance.component('RightToolbar', RightToolbar);
  // instance.use(VueAMap);
  instance.use(router);
  instance.use(store);
  instance.use(VXETable)
  instance.use(plugins);
  instance.use(elementIcons);
  instance.component('svg-icon', SvgIcon);
  directive(instance);

  // 使用element-plus 并且设置全局的大小
  instance.use(ElementPlus, {
    locale: zhCn,
    // 支持 large、default、small
    size: Cookies.get('size') || 'default',
  });
  instance.mount(container ? container.querySelector('#app') : '#app');
  console.log('实例挂载后');
}

if (!window.__POWERED_BY_QIANKUN__) {
  console.log('main.js文件中----独立运行');
  render();
}

export async function bootstrap() {
  console.log('子应用启动了');
}

export async function mount(props) {
  console.log(props, '<<<=== props 子应用激活挂载时主应用传过来的');
  props.onGlobalStateChange((state, previous) => {
    console.log('子应用监听全局变量改变');
    console.log(state, '<<<=== 子应用的state');
    console.log(previous, '<<<=== 子应用的previous');
    // 在permission仓库中将previous数据保存下来
    // usePermissionStore1.sysList=previous
  })

  render(props);
  instance.config.globalProperties.$onGlobalStateChange =
    props.onGlobalStateChange
  instance.config.globalProperties.$setGlobalState = props.setGlobalState;

  props.setGlobalState({
    name: '修改',
    subsysListGlobal: [],
  });

  // instance.config.globalProperties.$setGlobalState({
  //   name:'修改',
  //   subsysListGlobal:[]
  // })

  //   instance.config.globalProperties.$onGlobalStateChange((state,previous)=>{
  //   console.log('子应用监听全局变量改变2222222222');
  //   console.log(state,'<<<=== 子应用的state');
  //   console.log(previous,'<<<=== 子应用的previous');

  //   const usePermissionStore1=usePermissionStore()
  //   usePermissionStore1.sysList=previous.subsysListGlobal
  //   console.log(usePermissionStore1,'<<<=== usePermissionStore1----------------------------------------');
  // }

  // )
}

export async function unmount() {
  console.log('触发了子应用暴露出去的卸载方法==========unmount===============');
  instance.unmount();
  instance._container.innerHTML = '';
  instance = null;
  console.log(
    '触发了子应用暴露出去的卸载方法==========unmount===============卸载完成--------------',
  );
}
export async function update(props) {
  // 在需要更新应用程序时执行的操作，例如根据新的 props 进行重新渲染
}
